package com.suneast.suneaststore.modules.sort;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.bean.sort.Current;
import com.suneast.suneaststore.bean.sort.ItemBean;
import com.suneast.suneaststore.bean.sort.SubCate;
import com.suneast.suneaststore.bean.sort.TopCate;
import com.suneast.suneaststore.modules.sort.event.SortItemEvent;
import com.suneast.suneaststore.modules.sort.event.SortPageEvent;
import com.suneast.suneaststore.util.log.LatteLogger;

/**
 * Created by zestz on 2018/3/27.
 */

public final class SortDataParser {

    public static SortPageEvent parsePage(String jsonStr) {
        LatteLogger.json("test", jsonStr);
        SortPageEvent event = new SortPageEvent();
        JSONObject dataJObj = JSONObject.parseObject(jsonStr).getJSONObject("data");
        event.setImgUrl(dataJObj.getString("banner"));
        JSONArray topCateJArray = dataJObj.getJSONArray("topCate");
        for (int i = 0, length = topCateJArray.size(); i < length; i++) {
            JSONObject iJObj = topCateJArray.getJSONObject(i);
            event.getTopCateList().add(
                    new TopCate(
                            iJObj.getIntValue("id"),
                            iJObj.getString("name"),
                            iJObj.getString("type")
                    ));
        }
        JSONArray listJArray = dataJObj.getJSONArray("list");
        for (int i = 0, length = listJArray.size(); i < length; i++) {
            JSONObject jObj = listJArray.getJSONObject(i);
            ItemBean itemBean = new ItemBean();
            JSONObject cJObj = jObj.getJSONObject("current");
            Current current = new Current();
            current.setId(cJObj.getIntValue("id"));
            current.setName(cJObj.getString("name"));
            current.setParentId(cJObj.getIntValue("id"));
            current.setAttribute(cJObj.getString("name"));
            current.setShowAll(cJObj.getBooleanValue("showall"));
            itemBean.setCurrent(current);
            JSONArray subCateJArray = jObj.getJSONArray("subcate");
            for (int j = 0, jLength = subCateJArray.size(); j < jLength; j++) {
                JSONObject subCateJObj = subCateJArray.getJSONObject(j);
                SubCate subCate = new SubCate();
                subCate.setId(subCateJObj.getIntValue("id"));
                subCate.setName(subCateJObj.getString("name"));
                subCate.setAttributes(subCateJObj.getString("attributes"));
                subCate.setParentId(subCateJObj.getIntValue("parentid"));
                subCate.setThumb(subCateJObj.getString("thumb"));
                subCate.setTargetCate(subCateJObj.getIntValue("targetcate"));
                subCate.setType(subCateJObj.getIntValue("type"));
                subCate.setMerchId(subCateJObj.getIntValue("merchid"));
                JSONArray filtersArray = subCateJObj.getJSONArray("filters");
                if (filtersArray != null && filtersArray.size() > 0) {
                    LatteLogger.d("test", filtersArray.toJSONString());
                    subCate.setFilters(filtersArray.toJSONString());
                } else {
                    LatteLogger.d("test", "filers null");
                }

                itemBean.getSubCateList().add(subCate);
            }
            event.getItemBeanList().add(itemBean);
        }
        return event;
    }

    public static SortItemEvent parseItem(String jsonStr) {
        SortItemEvent event = new SortItemEvent();
        LatteLogger.json("test", jsonStr);
        JSONObject dataJObj = JSONObject.parseObject(jsonStr).getJSONObject("data");
        event.setImgUrl(dataJObj.getString("banner"));
        JSONArray listJArray = dataJObj.getJSONArray("list");
        for (int i = 0, length = listJArray.size(); i < length; i++) {
            JSONObject jObj = listJArray.getJSONObject(i);
            ItemBean itemBean = new ItemBean();
            JSONObject cJObj = jObj.getJSONObject("current");
            Current current = new Current();
            current.setId(cJObj.getIntValue("id"));
            current.setName(cJObj.getString("name"));
            current.setParentId(cJObj.getIntValue("id"));
            current.setAttribute(cJObj.getString("name"));
            current.setShowAll(cJObj.getBooleanValue("showall"));
            itemBean.setCurrent(current);
            JSONArray subCateJArray = jObj.getJSONArray("subcate");
            for (int j = 0, jLength = subCateJArray.size(); j < jLength; j++) {
                JSONObject subCateJObj = subCateJArray.getJSONObject(j);
                SubCate subCate = new SubCate();
                subCate.setId(subCateJObj.getIntValue("id"));
                subCate.setName(subCateJObj.getString("name"));
                subCate.setAttributes(subCateJObj.getString("attributes"));
                subCate.setParentId(subCateJObj.getIntValue("parentid"));
                subCate.setThumb(subCateJObj.getString("thumb"));
                subCate.setTargetCate(subCateJObj.getIntValue("targetcate"));
                subCate.setType(subCateJObj.getIntValue("type"));
                subCate.setMerchId(subCateJObj.getIntValue("merchid"));
                JSONArray filtersArray = subCateJObj.getJSONArray("filters");
                if (filtersArray != null && filtersArray.size() > 0) {
                    LatteLogger.d("test", filtersArray.toJSONString());
                    subCate.setFilters(filtersArray.toJSONString());
                } else {
                    LatteLogger.d("test", "filers null");
                }
                itemBean.getSubCateList().add(subCate);
            }
            event.getItemBeanList().add(itemBean);
        }
        return event;
    }
}
